import { configure } from "@testing-library/react"
import axios from "axios"
import {createBrowserHistory} from "history"
let history =createBrowserHistory()
let http=axios.create({
    baseURL:"/api",
    withCredentials:true,//携带凭证
    timeout:1000 * 20
})
// 请求拦截器
http.interceptors.request.use(config=>{
    // console.log(config)
    // 携带token过去
    let token=JSON.parse(localStorage.getItem("qf-token") as string)
    // let token = localStorage.getItem('qf-token') as string
    config.headers!.authorization=token as string
    return config
})
// 响应拦截器
http.interceptors.response.use(config=>{
    // console.log(config)
    if(config.data.code==="1004"){
        // 校验失败，登录过期
        // 提示登录过期
        alert("登录过期，请重新登录")
        //跳转到登入首页，清除token
        localStorage.removeItem("qf-token")
        history.push("/login")
        window.location.reload()

    }
    return config
})
export default http